﻿<%@ Page Title="My Spectrometers" Language="C#" MasterPageFile="~/MasterPage.master"
     AutoEventWireup="true" CodeFile="MySpectrometers.aspx.cs"
     ValidateRequest="false" Inherits="MySpectrometers" %>

<%-- Custom made page for screwturn wiki --%>
<asp:Content ID="Content1" ContentPlaceHolderID="CphMaster" runat="Server">
     <h1 class="pagetitlesystem">
          <asp:Literal ID="lblTitle" runat="server" meta:resourcekey="lblTitleResource1"
               Text="My Spectrometers" /></h1>
     <asp:MultiView ID="SpectrometersTools" runat="server" ActiveViewIndex="0">
          <asp:View ID="SpectrometersTableView" runat="server">
               <asp:Button runat="server" Text="Add" OnClick="AddSpectrometerButton_Click" />
               <hr />
               <br />
               <asp:GridView ID="SpectrometersTableGv" runat="server" AllowSorting="True"
                    AutoGenerateColumns="False" DataSourceID="SpectrometersDataSource"
                    EmptyDataText="There is no Spectrometers" DataKeyNames="Id"
                    Width="100%" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None"
                    BorderWidth="1px" CellPadding="3" GridLines="Horizontal"
                    OnRowCommand="SpectrometersTableGv_RowCommand">
                    <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                    <Columns>
                         <asp:ButtonField ButtonType="Link" DataTextField="Name" HeaderText="Name"
                              SortExpression="Name" CausesValidation="false" CommandName="showSelectedSpectrometerInfo"
                              ShowHeader="True">
                              <ItemStyle Width="100px" VerticalAlign="Top" />
                         </asp:ButtonField>
                         <asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location"
                              ItemStyle-Width="200px">
                              <ItemStyle Width="200px" VerticalAlign="Top" />
                         </asp:BoundField>
                         <asp:BoundField DataField="Short Description" HeaderText="Short Description"
                              SortExpression="Short Description">
                              <ItemStyle VerticalAlign="Top" />
                         </asp:BoundField>
                         <asp:TemplateField HeaderText="Contact" SortExpression="Contact">
                              <ItemTemplate>
                                   <asp:HyperLink runat="server" Text='<%# Eval("Contact") %>' NavigateUrl='<%# Eval("Email", "mailto://{0}") %>'
                                        ID="hlEmail" />
                              </ItemTemplate>
                              <ItemStyle Width="200px" VerticalAlign="Top" Font-Underline="true" />
                         </asp:TemplateField>
                         <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date"
                              ItemStyle-Width="100px" DataFormatString="{0:d}">
                              <ItemStyle HorizontalAlign="Center" VerticalAlign="Top" Width="100px" />
                         </asp:BoundField>
                         <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False"
                              SortExpression="Id" Visible="False" />
                         <asp:CommandField />
                    </Columns>
                    <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                    <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
                    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                    <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" HorizontalAlign="Left" />
                    <AlternatingRowStyle BackColor="#F7F7F7" />
               </asp:GridView>
               <asp:SqlDataSource ID="SpectrometersDataSource" runat="server"
                    ConnectionString="<%$ ConnectionStrings:NMRUserDataConnectionString %>"
                    SelectCommand="SELECT
	s1.id AS Id,
	s1.[date] AS [Date],
	s1.name AS Name,
	s1.memo AS [Short Description],
	s1.location AS Location,
	s1.contact AS Contact,
	s1.contactmail AS Email,
	s1.[version] AS [Version]
FROM (
	SELECT 
		s2.name, 
		MAX(s2.[version]) AS MaxVersion 
	FROM 
		spectrometers AS s2 
	INNER JOIN relation_spectrometers_to_users AS rsu 
		ON s2.id = rsu.spectrometerid 
	WHERE (rsu.username = @username) 
	GROUP BY s2.name) AS x
INNER JOIN spectrometers AS s1
	ON s1.name = x.name AND s1.[version]=x.MaxVersion
INNER JOIN relation_spectrometers_to_users AS rsu
	ON s1.id=rsu.spectrometerid
WHERE (rsu.username = @username)
	">
                    <SelectParameters>
                         <asp:Parameter Name="username" DefaultValue="none" />
                    </SelectParameters>
               </asp:SqlDataSource>
               <hr />
               <asp:Button runat="server" Text="Add" OnClick="AddSpectrometerButton_Click" />
          </asp:View>
          <asp:View ID="AddSpectrometerView" runat="server">
               <asp:Label ID="lblName" runat="server" Text="Name" Width="150px"></asp:Label>
               <asp:TextBox ID="tbName" runat="server" Columns="80" Width="550px"
                    Font-Size="14px" CssClass="InputField"></asp:TextBox>
               <asp:RequiredFieldValidator ID="valReqFieldName" runat="server"
                    ControlToValidate="tbName" Display="Dynamic" ErrorMessage="Name Must Be Present."
                    SetFocusOnError="True"></asp:RequiredFieldValidator>
               <asp:RegularExpressionValidator ID="RegularExpressionValidator1"
                    runat="server" ControlToValidate="tbName" Display="Dynamic"
                    ErrorMessage="Name must contain only Letters and Spaces."
                    ValidationExpression="[A-z0-9][0-9A-z\s]+" SetFocusOnError="True"></asp:RegularExpressionValidator>
               <asp:CustomValidator ID="UniqueNamecv" runat="server" ControlToValidate="tbName"
                    Display="Dynamic" EnableClientScript="False" ErrorMessage="Spectrometer with this name already exists."
                    OnServerValidate="UniqueNameValidate"></asp:CustomValidator>
               <br />
               <br />
               <asp:Label ID="lblLocation" runat="server" Text="Location" Width="150px"></asp:Label>
               <asp:TextBox ID="tbLocation" runat="server" Columns="80" Width="550px"
                    Font-Size="14px" CssClass="InputField"></asp:TextBox>
               <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
                    ControlToValidate="tbLocation" Display="Dynamic" ErrorMessage="Location Must Be Specified"></asp:RequiredFieldValidator>
               <asp:RegularExpressionValidator ID="RegularExpressionValidator2"
                    runat="server" ControlToValidate="tbLocation" Display="Dynamic"
                    ErrorMessage="Location can only contain letters, numbers and spaces."
                    ValidationExpression="[A-z0-9][A-z0-9\s,()/]+"></asp:RegularExpressionValidator>
               <br />
               <br />
               <asp:Label ID="lblMemo" runat="server" Text="Short Description"
                    Width="150px" Style="vertical-align: top"></asp:Label>
               <asp:TextBox ID="tbMemo" CssClass="InputField" runat="server"
                    MaxLength="2000" Rows="2" TextMode="MultiLine" Width="550px"
                    Font-Size="14px"></asp:TextBox>
               <asp:RegularExpressionValidator ID="RegularExpressionValidator3"
                    Style="vertical-align: top" runat="server" ControlToValidate="tbMemo"
                    Display="Dynamic" ErrorMessage="Short Description Should Not Contain &lt;&gt; Charachters."
                    ValidationExpression="[A-z0-9\s\\/:.,?!@#$;%^&amp;*()\\]*"></asp:RegularExpressionValidator>
               <br />
               <br />
               <asp:Panel runat="server" ID="UpdateInfopn" Style="display: none">
                    <asp:Label ID="lblVersionInfo" runat="server" Text="Version Specific Note"
                         Width="150px" Style="vertical-align: top"></asp:Label>
                    <asp:TextBox Font-Size="14px" ID="tbVersionInfo" CssClass="InputField"
                         runat="server" MaxLength="2000" Rows="2" TextMode="MultiLine"
                         Width="550px"></asp:TextBox>
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator5"
                         Style="vertical-align: top" runat="server" ControlToValidate="tbVersionInfo"
                         Display="Dynamic" ErrorMessage="Version Info Should Not Contain &lt;&gt; Charachters."
                         ValidationExpression="[A-z0-9\s\\/:.,?!@#$;%^&amp;*()\\]*"></asp:RegularExpressionValidator>
                    <br />
                    <br />
               </asp:Panel>
               <asp:Label ID="lblContact" runat="server" Text="Contact Person"
                    Width="150px"></asp:Label>
               <asp:TextBox ID="tbContactPerson" runat="server" Columns="80"
                    Font-Size="14px" CssClass="InputField" Width="550px"></asp:TextBox>
               <asp:RegularExpressionValidator ID="RegularExpressionValidator4"
                    runat="server" ControlToValidate="tbContactPerson" ErrorMessage="Contact Person Must be Specified."
                    ValidationExpression="[A-z\.\(\)/\s]+"></asp:RegularExpressionValidator>
               <br />
               <br />
               <asp:Label ID="lblContactEmail" runat="server" Text="Contact Email"
                    Width="150px"></asp:Label>
               <asp:TextBox ID="tbContactEmail" runat="server" Columns="80"
                    Font-Size="14px" CssClass="InputField" Width="550px"></asp:TextBox>
               <asp:RegularExpressionValidator ID="RegularExpressionValidator6"
                    runat="server" ControlToValidate="tbContactEmail" Display="Dynamic"
                    ErrorMessage="Email is not correct." ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
               <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
                    ControlToValidate="tbContactEmail" ErrorMessage="Email must be  specified."></asp:RequiredFieldValidator>
               <br />
               <br />
               <asp:Label ID="lblContent" runat="server" Text="Content" Width="150px"
                    Style="vertical-align: top"></asp:Label>
               <asp:TextBox ID="tbContent" runat="server" MaxLength="50000"
                    CssClass="InputField" Font-Size="14px" Rows="20" TextMode="MultiLine"
                    Width="550px"></asp:TextBox>
               <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="tbContent"
                    Style="vertical-align: top" EnableClientScript="False" ErrorMessage="Scripts Are Not Allowed."
                    OnServerValidate="ValidateForScripts" ValidateEmptyText="True"
                    Display="Dynamic"></asp:CustomValidator>
               <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
                    Style="vertical-align: top" ControlToValidate="tbContent"
                    Display="Dynamic" ErrorMessage="Content must present."></asp:RequiredFieldValidator>
               <br />
               <br />
               <asp:Button ID="butSubmit" runat="server" Text="Submit" OnCommand="butSubmit_Command" />
               <asp:Button ID="butClear" runat="server" CausesValidation="False"
                    OnClick="butClear_Click" Text="Clear" />
          </asp:View>
          <asp:View runat="server" EnableTheming="true">
               <p class="Boxy">
                    <asp:Label runat="server" ID="lblDate2" Text="Label Text"></asp:Label>
               </p>
               <p class="Boxy">
                    <asp:Label runat="server" ID="lblLocation2"></asp:Label></p>
               <p class="Boxy">
                    <asp:Label runat="server" ID="lblMemo2"></asp:Label></p>
               <p class="Boxy">
                    <asp:Label runat="server" ID="lblContact2"></asp:Label></p>
               <div style="margin: 0px 40px 0px 5px;">
                    <asp:Panel ID="Panel1" runat="server" CssClass="ContentBox">
                         Content:<br />
                         <asp:TextBox ID="tbContent2" Rows="40" runat="server" Wrap="false"
                              CssClass="ContentText" Text="Default" TextMode="MultiLine"
                              ReadOnly="True"></asp:TextBox>
                    </asp:Panel>
                    <asp:Panel ID="panPreviousVersions" CssClass="VersionsBox" runat="server">
                         <asp:Label runat="server" Text="<p><b>Versions:</b></p>"></asp:Label>
                         <asp:ListView ID="ListView1" runat="server" DataSourceID="PrevVersionsSQLDatasource"
                              DataKeyNames="SpecID">
                              <ItemTemplate>
                                   <li style="background-color: #DCDCDC; color: #000000;">
                                        <asp:LinkButton ID="VersionLinkButton" CommandArgument='<%# Eval("SpecID") %>'
                                             CommandName="ShowSpecWithID" runat="server" OnCommand="VersionLinkButton_Click">
                                         Version: <%# Eval("Version") %>
                                        </asp:LinkButton>
                                        created on <b>
                                             <asp:Label ID="DateLabel" runat="server" Text='<%# Eval("Date") %>' /></b>.
                                        <br />
                                        <asp:Label ID="InfoLabel" runat="server" Text='<%# Eval("Info") %>' />
                                        <br />
                                   </li>
                              </ItemTemplate>
                              <AlternatingItemTemplate>
                                   <li style="background-color: #CEE0E3; color: Black">
                                        <asp:LinkButton ID="VersionLinkButton" CommandArgument='<%# Eval("SpecID") %>'
                                             CommandName="ShowSpecWithID" runat="server" OnCommand="VersionLinkButton_Click">
                                         Version: <%# Eval("Version") %>
                                        </asp:LinkButton>
                                        created on <b>
                                             <asp:Label ID="DateLabel" runat="server" Text='<%# Eval("Date") %>' /></b>.
                                        <br />
                                        <asp:Label ID="InfoLabel" runat="server" Text='<%# Eval("Info") %>' />
                                        <br />
                                   </li>
                              </AlternatingItemTemplate>
                              <EmptyDataTemplate>
                                   There is no previous versions for this experiment.
                              </EmptyDataTemplate>
                              <InsertItemTemplate>
                                   <li style="">Version:
                                        <asp:TextBox ID="VersionTextBox" runat="server" Text='<%# Bind("Version") %>' />
                                        <br />
                                        Date:
                                        <asp:TextBox ID="DateTextBox" runat="server" Text='<%# Bind("Date") %>' />
                                        <br />
                                        Info:
                                        <asp:TextBox ID="InfoTextBox" runat="server" Text='<%# Bind("Info") %>' />
                                        <br />
                                        <asp:Button ID="InsertButton" runat="server" CommandName="Insert"
                                             Text="Insert" />
                                        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
                                             Text="Clear" />
                                   </li>
                              </InsertItemTemplate>
                              <LayoutTemplate>
                                   <ul id="itemPlaceholderContainer" runat="server" style="font-family: Verdana, Arial, Helvetica, sans-serif;">
                                        <li id="itemPlaceholder" runat="server" />
                                   </ul>
                                   <div style="text-align: center; background-color: #CCCCCC; font-family: Verdana, Arial, Helvetica, sans-serif;
                                        color: #000000;">
                                   </div>
                              </LayoutTemplate>
                              <EditItemTemplate>
                                   <li style="background-color: #008A8C; color: #FFFFFF;">SpecID:
                                        <asp:Label ID="SpecIDLabel1" runat="server" Text='<%# Eval("SpecID") %>' />
                                        <br />
                                        Version:
                                        <asp:TextBox ID="VersionTextBox" runat="server" Text='<%# Bind("Version") %>' />
                                        <br />
                                        Date:
                                        <asp:TextBox ID="DateTextBox" runat="server" Text='<%# Bind("Date") %>' />
                                        <br />
                                        Info:
                                        <asp:TextBox ID="InfoTextBox" runat="server" Text='<%# Bind("Info") %>' />
                                        <br />
                                        <asp:Button ID="UpdateButton" runat="server" CommandName="Update"
                                             Text="Update" />
                                        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
                                             Text="Cancel" />
                                   </li>
                              </EditItemTemplate>
                              <ItemSeparatorTemplate>
                                   <br />
                              </ItemSeparatorTemplate>
                              <SelectedItemTemplate>
                                   <li style="background-color: #008A8C; font-weight: bold; color: #FFFFFF;">
                                        SpecID:
                                        <asp:Label ID="SpecIDLabel" runat="server" Text='<%# Eval("SpecID") %>' />
                                        <br />
                                        Version:
                                        <asp:Label ID="VersionLabel" runat="server" Text='<%# Eval("Version") %>' />
                                        <br />
                                        Date:
                                        <asp:Label ID="DateLabel" runat="server" Text='<%# Eval("Date") %>' />
                                        <br />
                                        Info:
                                        <asp:Label ID="InfoLabel" runat="server" Text='<%# Eval("Info") %>' />
                                        <br />
                                   </li>
                              </SelectedItemTemplate>
                         </asp:ListView>
                    </asp:Panel>
               </div>
               <asp:SqlDataSource ID="PrevVersionsSQLDatasource" runat="server"
                    ConnectionString="<%$ ConnectionStrings:NMRUserDataConnectionString %>"
                    
                         SelectCommand="SELECT spectrometers.id AS SpecID, spectrometers.version AS Version, spectrometers.date AS Date, spectrometers.versioninfo AS Info FROM relation_spectrometers_to_users INNER JOIN spectrometers ON relation_spectrometers_to_users.spectrometerid = spectrometers.id WHERE (relation_spectrometers_to_users.username = @username) AND (spectrometers.name = (SELECT name FROM spectrometers AS spectrometers_1 WHERE (id = @id))) ORDER BY Version DESC">
                    <SelectParameters>
                         <asp:Parameter Name="username" Type="String" DefaultValue="none" />
                         <asp:Parameter Name="Id" DefaultValue="14" />
                    </SelectParameters>
               </asp:SqlDataSource>
               <asp:Panel runat="server" Style="clear: both" />
               <hr />
               <asp:Button ID="btnUpdate2" runat="server" Text="Update" CommandArgument="none"
                    OnCommand="btnUpdate2_Command" />
               <asp:Button ID="btnDelete2" runat="server" Text="Delete" OnClick="btnDelete2_Click" />
               <span runat="server" style="text-align: right">
                    <asp:Label runat="server" ID="lblDeleteQuestion" Text="Really Delete?"
                         Visible="false"></asp:Label>
                    <asp:Button ID="btnCancelDelete2" runat="server" Text="Cancel"
                         Visible="false" OnClick="btnCancelDelete2_Click" />
                    <asp:Button ID="btnRealyDelete2" runat="server" Text="Confirm Delete"
                         Visible="false" CommandArgument="none" CommandName="Delete"
                         OnCommand="btnRealyDelete2_Command" />
               </span>
          </asp:View>
     </asp:MultiView>
</asp:Content>
